/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution 
{
public:
    int dfs(TreeNode*root,int val)
    {
        if(root==nullptr) return 0;
        if(root->left==nullptr&&root->right==nullptr)
        {
            return val+root->val;
        }
        int l=dfs(root->left,(val+root->val)*10);
        int r=dfs(root->right,(val+root->val)*10);
        return l+r;
    }
    int sumNumbers(TreeNode* root) 
    {
        //设计一个函数头去dfs先求左树 再去求右树
        //最后返回两者之和
        return dfs(root,0);
    }
};